var UrlStore = function(){    
    /**
     * 添加url时.
     */
    this.on('addItem', function(item) {
        var type = item['type'];        
        if(type == 'survey'){
            surveyList.cardList.addItem(item);
                
        }
        if(type == 'eventurl'){
            eventList.cardList.addItem(item);
            if(false && page.pipe.requestCount > 2)
                ujs.browser.WebNotification.show({               
                    msg : 'A event is coming',
                    icon : '',
                    title : 'Aculearn'
                });    
        }
        if(type == 'web'){
            urlList.cardList.addItem(item);
            if(false && page.pipe.requestCount > 2)
                ujs.browser.WebNotification.show({               
                    msg : 'A link is coming',
                    icon : '',
                    title : 'Aculearn'
                });    
        }
        if(type == 'page'){
            pageList.cardList.addItem(item);
            if(false && page.pipe.requestCount > 2)
                ujs.browser.WebNotification.show({               
                    msg : 'A page is coming',
                    icon : '',
                    title : 'Aculearn'
                });    
        }        
        var previewFrame = new PreviewFrame({
            id : item['id'],
            src : item['url'],
            title : item['name'],
            perm : item['perm'],
            launchTime : item['launchTime'],
            relType : type
        });
        sessionList.addItem(item, false);
        page.contentCardLayout.addItem(item['id'], previewFrame);         
    });
        
    /**
     * 更新url时.
     */
    this.on('updateItem', function(item){
                
        var ifa = page.contentCardLayout.getActiveItem().$iframe
        if(ifa){
            ifa.get(0).contentWindow.location.reload()
        }
        
        //news
        if(item['perm'] && page.contentCardLayout.getActiveItem() && page.contentCardLayout.getActiveItem().data && page.contentCardLayout.getActiveItem().data.perm){
            if(item['perm']['vpar']){
                page.contentCardLayout.getActiveItem().toolbar.showItem('shows');
            } else {
                page.contentCardLayout.getActiveItem().toolbar.hideItem('shows');
            }
                        
            if(item['perm']['svy'] == 1){
                page.contentCardLayout.getActiveItem().toolbar.showItem('report');
            } else {
                page.contentCardLayout.getActiveItem().toolbar.hideItem('report');                
            }
        }
        
        var type = item['type'];        
        if(type == 'survey'){
            surveyList.cardList.addItem(item);    
        }
        if(type == 'eventurl'){
            eventList.cardList.addItem(item);    
        }
        if(type == 'web'){
            urlList.cardList.addItem(item);    
        }
        if(type == 'page'){
            pageList.cardList.addItem(item);    
        }        
        var $sItem = sessionList.$wrapper.find('.sessionListItem[sid=' + item.id + ']');
        if($sItem.length){
            if(item['name']){
                $sItem.find('.sessionTitle').text(item['name']);
            }
            if(item['icon']){
                $sItem.find('.sessionIconImg').attr('src', ujs.browser.loc.addQuery(item['icon'], {w: 200}));
            }
            if(item['launchTime']){
                $sItem.find('.unreadMsgCount').text(
                    $sItem.find('.unreadMsgCount').text().trim().replace('(Draft)', '')
                );
            }
        }
    });
    
    this.on('updateItem', function(item){
        sessionList.getSessionItem(item['id']).attr('changed', 1);   
    });
    
    /**
     * 删除url时.
     */
    this.on('deleteItem', function(itemID, item, rawItem){
        if(item){
            return ({
                survey : function(){
                    surveyList.cardList.deleteItem(itemID);    
                },
                eventurl : function(){
                    eventList.cardList.deleteItem(itemID);    
                },
                web : function(){
                    urlList.cardList.deleteItem(itemID);    
                },
                page : function(){
                    pageList.cardList.deleteItem(itemID);    
                }
            })[item['type']]();
        }              
    });
    //更改url.
    this.on('updateItem', function(newUrlInfo){
        var newUrl = '';
        var type = 'preview';
        
        if(newUrlInfo.url.url && newUrlInfo.url.url.startsWith('page://')){
            newUrl = '/ujs/app/pageDesigner/ui/survey/mobile.html?pageid={pageid}&eid={ueid}&t={time}&type={type}'.format({
                ueid : newUrlInfo['ueid'],
                pageid : newUrlInfo.url.url.substr(7),
                time : Date.now(),
                type : type
            });
        } else {
            newUrl = newUrlInfo.url.url;
        }
    });
}.extend(Store);